<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <FormItem label="流程名称" prop="name">
            <Input v-model="formValidate.name" placeholder="输入流程名称"></Input>
        </FormItem>
        <FormItem label="流程key" prop="key">
            <Input  v-model="formValidate.key" placeholder="输入流程key"></Input>
        </FormItem>
        <FormItem label="描述" prop="desc">
            <Input v-model="formValidate.desc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="请输入流程描述"></Input>
        </FormItem>
        <FormItem >
            <Button  @click="handleCancel()" style=" float: right; ">取消</Button>
            <Button  type="primary" @click="handlePost('formValidate')" style=" float: right; margin-right: 8px" >保存</Button>
        </FormItem>
    </Form>
</template>
<script>
    import api  from '../../libs/api';
    import {eventBus} from "../../libs/eventBus";
    import validate from '../../libs/validator';
    export default {
        name: 'model_add',
        data() {
            return {
                formValidate: {
                    name: '',
                    key: '',
                    desc:""
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '流程名称不允许为空', trigger: 'blur' }
                    ],
                    key: [
                        { required: true, message: '流程key不允许为空', trigger: 'blur' },
                    ],
                    desc: [
                        { required: true, message: '流程描述不允许为空', trigger: 'blur' }
                    ]
                }
            }
        },
        methods:{
            handlePost (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        this.$post(api.activity.models.base,this.formValidate).then(response => {
                            if(200==response.code){
                                this.$refs[name].resetFields();
                                eventBus.$emit("closeModal",false);
                                eventBus.$emit("freshenModelList");
                            }
                        });
                    }
                })
            },
            handleCancel(){
                this.$refs["formValidate"].resetFields();
                eventBus.$emit("closeModal",false);
            }

        }
    }
</script>